package com.mikepenz.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.mikepenz.app.R;
import com.mikepenz.bean.RequestBean;
import com.mikepenz.interactor.CurrentCarInteractor;
import com.mikepenz.presenter.listener.RequestListener;
import com.mikepenz.utils.MethodCode;
import com.mikepenz.utils.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/7.
 */
public class CurrentCarInteractorImp extends NetWorkImp implements CurrentCarInteractor {

    RequestListener<JSONObject> listener;

    public CurrentCarInteractorImp(Context c) {
        super(c);
    }

    public CurrentCarInteractorImp(Context c,RequestListener<JSONObject> listener){
        this(c);
        this.listener = listener;
    }


    @Override
    public void refreshCurrentCar(String userId, String cardId) {
        Map<String,Object> params = new HashMap<>();
        params.put("cardId", cardId);
        RequestBean bean = new RequestBean();
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.CURRENT_CAR);
        bean.setRequestParams(params);
        addFastJsonQueue(Request.Method.POST, MethodType.CURRENT_CAR, (JSONObject) JSON.toJSON(bean));
        startRequest();

    }

    @Override
    public void vehicles(String userId, String orderId) {
        Map<String,Object> params = new HashMap<>();
        params.put("ordercode", orderId);
        RequestBean bean = new RequestBean();
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.APPLICATION_RETURN);
        bean.setRequestParams(params);
        addFastJsonQueue(Request.Method.POST, MethodType.APPLICATION_RETURN,(JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        int statue = response.getInteger(MethodCode.responseStatue);

        if (statue == (MethodCode.NetState.SUCCESS.getValue())) {
                JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                listener.Success(tag + "",obj);
        }
        else
            listener.Error(tag + "", "当前没有正在租用的车辆");
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Error("",c.getString(R.string.system_error));
    }
}
